using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using JTLeigh.Tools.Development.CopySourceAsHtml.Properties;

namespace JTLeigh.Tools.Development.CopySourceAsHtml.UnitTests
{

    /// <summary>
    /// Unit tests for the <see cref="HtmlStyleSheet"/> class.
    /// </summary>
    [TestClass]
    public class HtmlStyleSheetFixture
    {
        #region Methods

        #region AddStyle

        /// <summary>
        /// Ensures <see cref="HtmlStyleSheet.AddStyle"/> correctly throw <see cref="ArgumentNullException"/> 
        /// when called with a <see langword="null"/> name.
        /// </summary>
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestAddStyleWithNullName()
        {
            HtmlStyleSheet sheet;

            sheet = new HtmlStyleSheet();
            sheet.AddStyle(null, new HtmlStyle());
        }

        /// <summary>
        /// Ensures <see cref="HtmlStyleSheet.AddStyle"/> correctly throw <see cref="ArgumentNullException"/> 
        /// when called with a <see langword="null"/> style.
        /// </summary>
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestAddStyleWithNullStyle()
        {
            HtmlStyleSheet sheet;

            sheet = new HtmlStyleSheet();
            sheet.AddStyle("style1", null);
        }

        /// <summary>
        /// Ensures <see cref="HtmlStyleSheet.AddStyle"/> adds the specified style to the 
        /// stylesheet.
        /// </summary>
        [TestMethod]
        public void TestAddStyle()
        {
            HtmlStyleSheet sheet;
            HtmlStyle style1;
            HtmlStyle style2;

            sheet = new HtmlStyleSheet();

            style1 = new HtmlStyle();
            style1.Append("content1");
            sheet.AddStyle("style1", style1);

            style2 = new HtmlStyle();
            style2.Append("content2");
            sheet.AddStyle("style2", style2);

            Assert.AreEqual("<style type=\"text/css\">\r\n.style1 { content1 }\r\n.style2 { content2 }\r\n</style>\r\n", sheet.GetHtml(false));
            Assert.AreEqual("<style type=\"text/css\">.style1 { content1 }.style2 { content2 }</style>", sheet.GetHtml(true));
        }

        #endregion // AddStyle

        #region AddReusableStyle

        /// <summary>
        /// Ensures <see cref="HtmlStyleSheet.AddReusableStyle"/> correctly throws <see cref="ArgumentNullException"/> 
        /// when called with a <see langword="null"/> prefix.
        /// </summary>
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestAddReusableStyleWithNullPrefix()
        {
            HtmlStyleSheet sheet;

            sheet = new HtmlStyleSheet();
            sheet.AddReusableStyle(null, new HtmlStyle());
        }

        /// <summary>
        /// Ensures <see cref="HtmlStyleSheet.AddReusableStyle"/> correctly throws <see cref="ArgumentNullException"/> 
        /// when called with a <see langword="null"/> style.
        /// </summary>
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestAddReusableStyleWithNullStyle()
        {
            HtmlStyleSheet sheet;

            sheet = new HtmlStyleSheet();
            sheet.AddReusableStyle("style", null);
        }

        /// <summary>
        /// Ensures <see cref="HtmlStyleSheet.AddReusableStyle"/> adds the specified style to the 
        /// stylesheet, reusing existing styles and incrementing style numbers correctly.
        /// </summary>
        [TestMethod]
        public void TestAddReusableStyle()
        {
            HtmlStyleSheet sheet;
            HtmlStyle style1;
            HtmlStyle style2;
            HtmlStyle style3;

            sheet = new HtmlStyleSheet();

            style1 = new HtmlStyle();
            style1.Append("content1");
            sheet.AddReusableStyle("style", style1); // should be added as "style1".
            sheet.AddReusableStyle("style", style1); // 2nd time shouldn't be added.

            style2 = new HtmlStyle();
            style2.Append("content2");
            sheet.AddReusableStyle("style", style2); // should be added as "style2".
            sheet.AddReusableStyle("style", style2); // 2nd time shouldn't be added.

            style3 = new HtmlStyle();
            style3.Append("content1");
            sheet.AddReusableStyle("other", style3); // should be added as "other1".

            Assert.AreEqual("<style type=\"text/css\">\r\n.other1 { content1 }\r\n.style1 { content1 }\r\n.style2 { content2 }\r\n</style>\r\n", sheet.GetHtml(false));
            Assert.AreEqual("<style type=\"text/css\">.other1 { content1 }.style1 { content1 }.style2 { content2 }</style>", sheet.GetHtml(true));
        }

        #endregion // AddReusableStyle

        #endregion // Methods
    }

}
